<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>if...elif...else语句的使用 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="section04.html" />
    
    
    <link rel="prev" href="section02.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="1.3"
        data-chapter-title="if...elif...else语句的使用"
        data-filepath="01/section03.md"
        data-basepath=".."
        data-revision="Mon Feb 03 2020 14:44:53 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="../chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        条件判断语句
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01/section01.html">
            
                
                    <a href="section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        if语句的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01/section02.html">
            
                
                    <a href="section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        if...else语句的使用
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="01/section03.html">
            
                
                    <a href="../01/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        if...elif...else语句的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="01/section04.html">
            
                
                    <a href="section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        if语句嵌套
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="01/section05.html">
            
                
                    <a href="section05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        条件判断练习:猜拳游戏
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="01/section06.html">
            
                
                    <a href="section06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        条件判断语句补充
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="../chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        循环语句
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="02/section01.html">
            
                
                    <a href="../02/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        while语句的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="02/section02.html">
            
                
                    <a href="../02/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        for语句的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="02/section03.html">
            
                
                    <a href="../02/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        break和continue的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="02/section04.html">
            
                
                    <a href="../02/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        嵌套循环
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="02/section05.html">
            
                
                    <a href="../02/section05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        for...else语句的使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="elif">elif</h2>
<ul>
<li><p>&#x60F3;&#x4E00;&#x60F3;:</p>
<blockquote>
<p>&#x5982;&#x679C;&#x6709;&#x8FD9;&#x6837;&#x4E00;&#x79CD;&#x60C5;&#x51B5;&#xFF1A;&#x5F53;&#x6761;&#x4EF6;A&#x6EE1;&#x8DB3;&#x65F6;&#x505A;&#x4E8B;&#x60C5;1&#xFF1B;&#x5F53;&#x6761;&#x4EF6;A&#x4E0D;&#x6EE1;&#x8DB3;&#x3001;&#x6761;&#x4EF6;B&#x6EE1;&#x8DB3;&#x65F6;&#x505A;&#x4E8B;&#x60C5;2&#xFF1B;&#x5F53;&#x6761;&#x4EF6;B&#x4E0D;&#x6EE1;&#x8DB3;&#x3001;&#x6761;&#x4EF6;C&#x6EE1;&#x8DB3;&#x65F6;&#x505A;&#x4E8B;&#x60C5;3&#xFF0C;&#x90A3;&#x8BE5;&#x600E;&#x4E48;&#x5B9E;&#x73B0;&#x5462;&#xFF1F;</p>
</blockquote>
</li>
<li><p>&#x7B54;:</p>
<blockquote>
<p>elif</p>
</blockquote>
</li>
</ul>
<h3 id="&#x4E00;&#x3001;-elif&#x7684;&#x529F;&#x80FD;">&#x4E00;&#x3001; elif&#x7684;&#x529F;&#x80FD;</h3>
<p>elif&#x7684;&#x4F7F;&#x7528;&#x683C;&#x5F0F;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-python">    <span class="hljs-keyword">if</span> xxx1:
        &#x4E8B;&#x60C5;<span class="hljs-number">1</span>
    <span class="hljs-keyword">elif</span> xxx2:
        &#x4E8B;&#x60C5;<span class="hljs-number">2</span>
    <span class="hljs-keyword">elif</span> xxx3:
        &#x4E8B;&#x60C5;<span class="hljs-number">3</span>
</code></pre>
<p>&#x8BF4;&#x660E;:</p>
<ul>
<li>&#x5F53;xxx1&#x6EE1;&#x8DB3;&#x65F6;&#xFF0C;&#x6267;&#x884C;&#x4E8B;&#x60C5;1&#xFF0C;&#x7136;&#x540E;&#x6574;&#x4E2A;if&#x7ED3;&#x675F;</li>
<li>&#x5F53;xxx1&#x4E0D;&#x6EE1;&#x8DB3;&#x65F6;&#xFF0C;&#x90A3;&#x4E48;&#x5224;&#x65AD;xxx2&#xFF0C;&#x5982;&#x679C;xxx2&#x6EE1;&#x8DB3;&#xFF0C;&#x5219;&#x6267;&#x884C;&#x4E8B;&#x60C5;2&#xFF0C;&#x7136;&#x540E;&#x6574;&#x4E2A;if&#x7ED3;&#x675F;</li>
<li>&#x5F53;xxx1&#x4E0D;&#x6EE1;&#x8DB3;&#x65F6;&#xFF0C;xxx2&#x4E5F;&#x4E0D;&#x6EE1;&#x8DB3;&#xFF0C;&#x5982;&#x679C;xxx3&#x6EE1;&#x8DB3;&#xFF0C;&#x5219;&#x6267;&#x884C;&#x4E8B;&#x60C5;3&#xFF0C;&#x7136;&#x540E;&#x6574;&#x4E2A;if&#x7ED3;&#x675F;</li>
</ul>
<p>demo:</p>
<pre><code class="lang-python">    score = <span class="hljs-number">77</span>

    <span class="hljs-keyword">if</span> score&gt;=<span class="hljs-number">90</span> <span class="hljs-keyword">and</span> score&lt;=<span class="hljs-number">100</span>:
        print(<span class="hljs-string">&apos;&#x672C;&#x6B21;&#x8003;&#x8BD5;&#xFF0C;&#x7B49;&#x7EA7;&#x4E3A;A&apos;</span>)
    <span class="hljs-keyword">elif</span> score&gt;=<span class="hljs-number">80</span> <span class="hljs-keyword">and</span> score&lt;<span class="hljs-number">90</span>:
        print(<span class="hljs-string">&apos;&#x672C;&#x6B21;&#x8003;&#x8BD5;&#xFF0C;&#x7B49;&#x7EA7;&#x4E3A;B&apos;</span>)
    <span class="hljs-keyword">elif</span> score&gt;=<span class="hljs-number">70</span> <span class="hljs-keyword">and</span> score&lt;<span class="hljs-number">80</span>:
        print(<span class="hljs-string">&apos;&#x672C;&#x6B21;&#x8003;&#x8BD5;&#xFF0C;&#x7B49;&#x7EA7;&#x4E3A;C&apos;</span>)
    <span class="hljs-keyword">elif</span> score&gt;=<span class="hljs-number">60</span> <span class="hljs-keyword">and</span> score&lt;<span class="hljs-number">70</span>:
        print(<span class="hljs-string">&apos;&#x672C;&#x6B21;&#x8003;&#x8BD5;&#xFF0C;&#x7B49;&#x7EA7;&#x4E3A;D&apos;</span>)
    <span class="hljs-keyword">elif</span> score&gt;=<span class="hljs-number">0</span> <span class="hljs-keyword">and</span> score&lt;<span class="hljs-number">60</span>:
        print(<span class="hljs-string">&apos;&#x672C;&#x6B21;&#x8003;&#x8BD5;&#xFF0C;&#x7B49;&#x7EA7;&#x4E3A;E&apos;</span>)
</code></pre>
<h3 id="&#x4E8C;&#x3001;&#x6CE8;&#x610F;&#x70B9;">&#x4E8C;&#x3001;&#x6CE8;&#x610F;&#x70B9;</h3>
<ul>
<li><p>&#x53EF;&#x4EE5;&#x548C;else&#x4E00;&#x8D77;&#x4F7F;&#x7528;</p>
<pre><code class="lang-python">   <span class="hljs-keyword">if</span> &#x6027;&#x522B;&#x4E3A;&#x7537;&#x6027;:
       &#x8F93;&#x51FA;&#x7537;&#x6027;&#x7684;&#x4F53;&#x91CD;
       ...
   <span class="hljs-keyword">elif</span> &#x6027;&#x522B;&#x4E3A;&#x5973;&#x6027;:
       &#x8F93;&#x51FA;&#x5973;&#x6027;&#x7684;&#x4F53;&#x91CD;
       ...
   <span class="hljs-keyword">else</span>:
       &#x7B2C;&#x4E09;&#x79CD;&#x6027;&#x522B;&#x7684;&#x4F53;&#x91CD;
       ...
</code></pre>
<p>&#x8BF4;&#x660E;:</p>
<ul>
<li>&#x5F53; &#x201C;&#x6027;&#x522B;&#x4E3A;&#x7537;&#x6027;&#x201D; &#x6EE1;&#x8DB3;&#x65F6;&#xFF0C;&#x6267;&#x884C; &#x201C;&#x8F93;&#x51FA;&#x7537;&#x6027;&#x7684;&#x4F53;&#x91CD;&#x201D;&#x7684;&#x76F8;&#x5173;&#x4EE3;&#x7801;</li>
<li>&#x5F53; &#x201C;&#x6027;&#x522B;&#x4E3A;&#x7537;&#x6027;&#x201D; &#x4E0D;&#x6EE1;&#x8DB3;&#x65F6;&#xFF0C;&#x5982;&#x679C; &#x201C;&#x6027;&#x522B;&#x4E3A;&#x5973;&#x6027;&#x201D;&#x6EE1;&#x8DB3;&#xFF0C;&#x5219;&#x6267;&#x884C; &#x201C;&#x8F93;&#x51FA;&#x5973;&#x6027;&#x7684;&#x4F53;&#x91CD;&#x201D;&#x7684;&#x76F8;&#x5173;&#x4EE3;&#x7801;</li>
<li>&#x5F53; &#x201C;&#x6027;&#x522B;&#x4E3A;&#x7537;&#x6027;&#x201D; &#x4E0D;&#x6EE1;&#x8DB3;&#xFF0C;&#x201C;&#x6027;&#x522B;&#x4E3A;&#x5973;&#x6027;&#x201D;&#x4E5F;&#x4E0D;&#x6EE1;&#x8DB3;&#xFF0C;&#x90A3;&#x4E48;&#x4E45;&#x9ED8;&#x8BA4;&#x6267;&#x884C;else&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5373; &#x201C;&#x7B2C;&#x4E09;&#x79CD;&#x6027;&#x522B;&#x7684;&#x4F53;&#x91CD;&#x201D;&#x76F8;&#x5173;&#x4EE3;&#x7801;</li>
</ul>
</li>
</ul>
<blockquote>
<p>elif&#x5FC5;&#x987B;&#x548C;if&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#xFF0C;&#x5426;&#x5219;&#x51FA;&#x9519; else &#x4E00;&#x822C;&#x7528;&#x5728;&#x6700;&#x540E;&#xFF0C;&#x5373;&#x6240;&#x6709;&#x6761;&#x4EF6;&#x90FD;&#x4E0D;&#x6EE1;&#x8DB3;&#x65F6;&#x4F7F;&#x7528;</p>
</blockquote>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="section02.html" class="navigation navigation-prev " aria-label="Previous page: if...else语句的使用"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="section04.html" class="navigation navigation-next " aria-label="Next page: if语句嵌套"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
